<?php

	namespace model;
		// Kanske INTE behövs längre
		//header("Cache-Control: no-cache, must-revalidate");

		session_start();

		require_once ("User.php");
		require_once ("Random_string.php");

		class LoginModel
		{
			//Strings
			private $correctName = "Admin";
			private $correctPassW = "123";
			private $userName;
			private $userPassW;
			private $myRandomPassword;
			private $session;
			private $message = "";
			
			//User
			private $user;
			//Bool
			private $userIsLoggedIn = false;
			private $status;

			public function __construct()
			{
			}
			
			//return bool
			public function isUserLoggedIn()
			{
				return $this->userIsLoggedIn;
			}

			//Returns string message
			public function getMessage()
			{
				return $this->message;
			}
			
			public function logOutUser()
			{
				$this->userIsLoggedIn = false;
				return false;
			}					

			//Returns string message
			public function getStatus()
			{
					
				if 	($this->status = true)
				{
					return "Inloggad";
				}
				return "Ej Inloggad";
			}


			public function getValidatedUserPassW()
			{
				return $this->validatedUserPassW;
			}


			public function getValidatedUserName()
			{
				return $this->validatedUserName;
			}

			//@param  User $user
			public function validateUser(User $user)
			{

				$this->userName = $user->getUserInputName();
				$this->userPassW = $user->getUserInputPassw();

				//If values are correct
				if ($this->userName == $this->correctName && $this->userPassW == $this->correctPassW)
				{
					$this->validatedUserName = $this->userName;
					$this->validatedUserPassW = $this->userPassW;
					$this->userIsLoggedIn = true;
					return true;
				}
				
				else if ($this->userPassW == $this->correctPassW && $this->userName != $this->correctName)
				{
					$this->status = false;
					$this->userIsLoggedIn = false;
					
					throw new \Exception("Fel användarnamn  och/eller lösenord");
				
				}
				
				else if ($this->userName == $this->correctName && $this->userPassW != $this->correctPassW)
				{
					$this->status = false;
					$this->userIsLoggedIn = false;
					throw new \Exception("Fel lösenord och/eller användarnamn");
				}

				else
				{
					$this->status = false;
					$this->userIsLoggedIn = false;
					throw new \Exception("Fel användaruppgifter");
				}
			}

			public function getUsersLogInPreference()
			{
				//If user chooses to stay logged in (Checked box)
				
				//TODO ta bort beroende tioo POST här
				if (isset($_POST["CheckBox"]) && $_POST["CheckBox"] == 'yes')
				{
					return true;
				}
				return false;
			}


			public function getRandomPassword()
			{
				$this->myRandomPassword = new \model\Random_string();
				$this->tempCorrectPassW = $this->myRandomPassword->createRandomString();

				return $this->tempCorrectPassW;
			}

		}
